package com.egao.common.manage.company;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.Constants;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.entity.vo.StudentSignAdminListVO;
import com.egao.common.module.student.entity.vo.StudentSignCompanyListVO;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.student.service.StudentSignService;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 单位签约管理
 * @author lym
 */
@Api(tags = "单位签约管理")
@RestController
@RequestMapping("${api}")
public class CompanySignController extends BaseController {

    @Autowired
    private StudentSignService studentSignService;

    private final String title = "单位签约管理";

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "分页查询毕业去向")
    @ApiOperation(value = "分页查询毕业去向")
    @ApiPageParam
    @GetMapping("/company/sign/page")
    public PageResult<StudentSignCompanyListVO> page(HttpServletRequest request) {
        PageParam<StudentSignCompanyListVO> pageParam = new PageParam<>(request);
        pageParam.put("loginUserId", getLoginUserId());
        return studentSignService.listCompanyPage(pageParam);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "查询详情")
    @ApiOperation("查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Integer")
    })
    @GetMapping("/company/sign/detail")
    public JsonResult detail(Integer id) {
        JsonResult jsonResult = studentSignService.verifyCompanyView(id, getLoginUserId());
        if (Constants.RESULT_ERROR_CODE == jsonResult.getCode()) {
            return jsonResult;
        }

        return studentSignService.detail(id, null, false, getLoginUser().getUserType());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "检索学生")
    @ApiOperation(value = "检索学生", notes = "根据学号检索学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xh", value = "学号", required = true, dataType = "String")
    })
    @GetMapping("/company/sign/search")
    public JsonResult search(String xh) {
        return studentSignService.searchStudent(xh, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "选择录用方式", result = true)
    @ApiOperation(value = "选择录用方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSign", value = "studentSign", required = true, dataType = "studentSign")
    })
    @PostMapping("/company/sign/pick-dispatch-hired-way")
    public JsonResult pickDispatchHiredWay(@RequestBody StudentSign studentSign) {
        return studentSignService.companyPickDispatchHiredWay(studentSign, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "初始化表单")
    @ApiOperation(value = "初始化表单")
    @GetMapping("/company/sign/form")
    public JsonResult form() {
        return studentSignService.companySignForm(getLoginUserId());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "邀约", result = true)
    @ApiOperation("邀约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSign", value = "studentSign", required = true, dataType = "studentSign")
    })
    @PostMapping("/company/sign/submit")
    public JsonResult submit(@RequestBody StudentSign studentSign) {
        return studentSignService.submitForCompany(studentSign, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = title, desc = "修改邀约", result = true)
    @ApiOperation("修改邀约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSign", value = "studentSign", required = true, dataType = "studentSign")
    })
    @PostMapping("/company/sign/update")
    public JsonResult update(@RequestBody StudentSign studentSign) {
        return studentSignService.submitForCompany(studentSign, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation("协议就业打印表")
    @ApiPageParam
    @PostMapping("/company/sign/print/deal")
    public JsonResult dealPrint(@RequestBody StudentSign studentSign) {
        if (studentSign == null || studentSign.getId() == null) {
            throw new BusinessException("参数异常");
        }

        JsonResult jsonResult = studentSignService.verifyCompanyView(studentSign.getId(), getLoginUserId());
        if (Constants.RESULT_ERROR_CODE == jsonResult.getCode()) {
            return jsonResult;
        }

        return studentSignService.dealPrint(studentSign.getId(), null);
    }
}
